import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author 35326
 * 2021/3/16 20:15
 *
 * 这个类没有什么意义，是用来停滞在这里，用来运行JVM监控工具的
 */
public class CommonTest {
    public static void main(String[] args) {
//        SemaphoreDemo semaphoreDemo = new SemaphoreDemo();
//        ExecutorService executorService = Executors.newFixedThreadPool(20);
////        new ThreadPoolExecutor()
//        for (int i = 0; i < 20; i++) {
//            executorService.submit(semaphoreDemo);
//        }
//        executorService.shutdown();

        ExecutorService executorService = Executors.newCachedThreadPool();
        for(;;){
            executorService.submit(new Thread(){
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000000000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    private static class SimpleWN{
        final static Object object = new Object();
        public static class T1 extends Thread{
            @Override
            public void run() {
                synchronized (object){
                    System.out.println(System.currentTimeMillis()+": T1 start!");
                    try{
                        System.out.println(System.currentTimeMillis()+": T1 wait for object!");
                        object.wait();
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println(System.currentTimeMillis()+": T1 end!");
                }
            }
        }

        public static class T2 extends Thread{
            @Override
            public void run() {
                synchronized (object){
                    System.out.println(System.currentTimeMillis()+": T2 start! notify one thread!");
                    object.notify();
                    System.out.println(System.currentTimeMillis()+": T2 end!");
                    try {
                        Thread.sleep(2000);
                    }catch (InterruptedException e){

                    }
                }
            }
        }
    }

    @Test
    public void test333(){
        Thread t1 = new SimpleWN.T1();
        Thread t2=new SimpleWN.T2();
        t1.start();
        t2.start();
    }

    @Test
    public void testTest(){
        SemaphoreDemo semaphoreDemo = new SemaphoreDemo();
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 20; i++) {
            executorService.submit(semaphoreDemo);
        }
        executorService.shutdown();
    }

    @Test
    public void testThreadPool(){
        ExecutorService executorService = Executors.newCachedThreadPool();
        for(;;){
            executorService.submit(new Thread(){
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000000000);
                        Math.pow(Math.random(),Math.random());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    @Test
    public void testt(){
        System.out.println(Runtime.getRuntime().availableProcessors());
    }



}

class SemaphoreDemo implements  Runnable{
    final Semaphore semp = new Semaphore(5);

    @Override
    public void run() {
        try {
            semp.acquire();
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getId()+": done!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            semp.release();
        }
    }
}


